/**
 * // Definition for a Node.
 * function Node(val, left, right, next) {
 *    this.val = val === undefined ? null : val;
 *    this.left = left === undefined ? null : left;
 *    this.right = right === undefined ? null : right;
 *    this.next = next === undefined ? null : next;
 * };
 */

/**
 * @func connect
 * @param {Node} root
 * @return {Node}
 * @author lx
 */
var connect = function (root) {
    if (!root) return []
    let queue = [root]
    let res = []

    while (queue.length) {
        let len = queue.length
        for (let i = 0; i < len; i++) {
            let node = queue.shift()
            if(i === len - 1){
                node.next = null
            }else{
                node.next = queue[0]
            }
            if(node.left){
                queue.push(node.left)
            }
            if(node.right){
                queue.push(node.right)
            }
            res.push(node.val)
        }
    }
    return res
};